home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / Libr / getopt.c < prev    next >
C/C++ Source or Header  |  1990-04-19  |  2KB  |  71 lines

  1.  
  2. #ifdef PDC
  3. #include    <xstdio.h>
  4. #else
  5. #include     <stdio.h>
  6. #endif
  7.  
  8. #define ERR(spat, cpat)   \
  9.     if (opterr) {    errbuf[0] = cpat; errbuf[1] = '\n';        \
  10.     (void) fwrite( argv[0], 1, strlen(argv[0]), stderr);    \
  11.     (void) fwrite( spat, 1, strlen(spat), stderr);                \
  12.     (void) fwrite( errbuf, 1, 2, stderr); }
  13.  
  14. extern int      strcmp();
  15. extern char    *index();
  16. extern int      strlen();
  17.  
  18. int             opterr = 1;
  19. int             optind = 1;
  20. int             optopt;
  21. char           *optarg;
  22.  
  23. int
  24. getopt(argc, argv, opts)
  25.     int             argc;
  26.     char          **argv, *opts;
  27. {
  28.     static int      sp = 1;
  29.     register int    c;
  30.     register char  *cp;
  31.     char            errbuf[2];
  32.  
  33.     if (sp == 1)
  34.         if (optind >= argc ||
  35.             argv[optind][0] != '-' || argv[optind][1] == '\0')
  36.             return (EOF);
  37.         else if (strcmp(argv[optind], "--") == NULL) {
  38.             optind++;
  39.             return (EOF);
  40.         }
  41.     optopt = c = argv[optind][sp];
  42.     if (c == ':' || (cp = index(opts, c)) == NULL) {
  43.         ERR(": illegal option -- ", c);
  44.         if (argv[optind][++sp] == '\0') {
  45.             optind++;
  46.             sp = 1;
  47.         }
  48.         return ('?');
  49.     }
  50.     if (*++cp == ':') {
  51.         if (argv[optind][sp + 1] != '\0')
  52.             optarg = &argv[optind++][sp + 1];
  53.         else if (++optind >= argc) {
  54.             ERR(": option requires an argument -- ", c);
  55.             sp = 1;
  56.             return ('?');
  57.         }
  58.         else
  59.             optarg = argv[optind++];
  60.         sp = 1;
  61.     }
  62.     else {
  63.         if (argv[optind][++sp] == '\0') {
  64.             sp = 1;
  65.             optind++;
  66.         }
  67.         optarg = NULL;
  68.     }
  69.     return (c);
  70. }
  71.